﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using DevExpress.Web.ASPxMenu;
using System.Xml;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;


namespace SGDemo.Permission
{
    public partial class PageList : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);
            BindComBoBox();
         
            
        }

        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveFastAdd()
        {
            string userkeyid = ApplicationContext.Current.UserKeyid;
            string pagecode = this.GetType().Name;
            PageEntity page = _permissionService.Page_GetKeyidBypagename(pagecode);
            if (page != null)
            {
                return _permissionService.Shortcuts_Add(userkeyid, page.Keyid);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 模块的绑定下拉框
        /// </summary>
        public void BindComBoBox()
        {
            List<ModuleEntity> moduleList = _permissionService.Module_GetAll();
            foreach (ModuleEntity list in moduleList)
            {
                ListEditItem  listbox = new ListEditItem ();
                listbox.Text = list.ModuleName;
                listbox.Value = list.Keyid;
                cbModule.Items.Add(listbox);

            }
           
        }

        /// <summary>
        /// 自定义GridView回调重新绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            gvwList.PageIndex = 0;
            gvwList.DataBind();
            gvwList.Selection.UnselectAll();//不选中任何一行
        }

        /// <summary>
        /// 菜单项绑定事件 - 实现操作权限控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void menuHeader_ItemDataBound(object sender, MenuItemEventArgs e)
        {
            IHierarchyData itemHierarchyData = (IHierarchyData)e.Item.DataItem;
            XmlElement element = (XmlElement)itemHierarchyData.Item;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyids">Keyid</param>
        /// <returns>是否删除成功</returns>
        [AjaxPro.AjaxMethod]
        public bool Delete(string keyids)
        {

            return _permissionService.BulkDeletePage(keyids);
        }

        /// <summary>
        /// 导出所有页面
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string ExportPageList()
        {
            List<ModuleEntity> modules = _permissionService.Module_GetAll();

            #region 声明文档

            XmlDocument xd = new XmlDocument();

            #endregion 

            #region 添加文档声明

            XmlDeclaration xde = xd.CreateXmlDeclaration("1.0", "UTF-8", null);
            xd.AppendChild(xde);
 
            #endregion

            #region 添加根节点

            XmlElement xe = xd.CreateElement("AllPage");//创建一个根元素  
            xd.AppendChild(xe);//根元素创建完成  

            #endregion

            #region  添加非根节点

            XmlNode root = xd.SelectSingleNode("AllPage");
            foreach (ModuleEntity m in modules)
            {
                XmlElement firstLevelChild = xd.CreateElement("Module");
                firstLevelChild.SetAttribute("ModuleName", m.ModuleName);
                firstLevelChild.SetAttribute("ModuleCode", m.ModuleCode);
                firstLevelChild.SetAttribute("ModuleNameEn", m.ModuleNameEn);
                firstLevelChild.SetAttribute("Order", m.Order.ToString());
                firstLevelChild.SetAttribute("NavigateUrl", m.NavigateUrl);
                firstLevelChild.SetAttribute("Keyid", m.Keyid);
                root.AppendChild(firstLevelChild);

                List<PageEntity> pages = _permissionService.Page_GetByModuleKeyid(m.Keyid);
                foreach(PageEntity p in pages)
                {
                    XmlElement secondLevelChild = xd.CreateElement("Page");
                    secondLevelChild.SetAttribute("PageName", p.PageName);
                    secondLevelChild.SetAttribute("PageCode", p.PageCode);
                    secondLevelChild.SetAttribute("PageNameEn", p.PageNameEn);
                    secondLevelChild.SetAttribute("NavigateUrl", p.NavigateUrl);
                    secondLevelChild.SetAttribute("Order", p.Order == null ? "0" : p.Order.ToString());
                    secondLevelChild.SetAttribute("PageType", p.PageType);
                    secondLevelChild.SetAttribute("Remark", p.Remark);
                    secondLevelChild.SetAttribute("ImageUrl", p.ImageUrl);

                    secondLevelChild.SetAttribute("ParentKeyid", p.ParentKeyid);
                    secondLevelChild.SetAttribute("Keyid", p.Keyid);
                    secondLevelChild.SetAttribute("ModuleKeyid", p.ModuleKeyid);
                    secondLevelChild.SetAttribute("HelpKeyid", p.HelpKeyid);
                    secondLevelChild.SetAttribute("HelpTitle", p.HelpTitle);
                    
                    firstLevelChild.AppendChild(secondLevelChild);
                }
            }

            #endregion

            xd.Save(Server.MapPath("~/UploadFile/Permission/PageList.xml"));
            return "UploadFile/Permission/PageList.xml";
        }
    }
    public class PageManage
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<PageEntity> PagingGetPage(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            return _permissionService.Page_PagingGet(startRecord, maxRecords, sortColumns, filterConditions);
        }

        public int PagingGetPageCount(String filterConditions)
        {
            return _permissionService.Page_PagingGetCount(filterConditions);
        }
    }
}